package com.sinosoft.dsp.platform.viewbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.CloseEvent;
import org.primefaces.event.DashboardReorderEvent;
import org.primefaces.model.DashboardColumn;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;

import com.sinosoft.dsp.platform.components.IViewBean;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.platform.services.PlatformViewService;
import com.sinosoft.dsp.system.utils.Const;

/**
 * Home页ViewBean
 * 
 * @author 潘巍（Peter Pan）
 * @since 2010-10-26 上午06:10:04
 */
@ManagedBean(name = "homeViewListBeanService")
@RequestScoped
public class HomeViewListBeanService {

	private int moduleColumn = Const.CONST_MAINVIEW_TAB_HOME_MODULESET_COLUMN;

	private DashboardModel model;

	@ManagedProperty(value = "#{platformViewService}")
	private PlatformViewService viewService;

	private List<IViewBean> views;

	public void handleReorder(DashboardReorderEvent event) {
		FacesMessage message = new FacesMessage();
		message.setSeverity(FacesMessage.SEVERITY_INFO);
		message.setSummary("Reordered: " + event.getWidgetId());
		message.setDetail("Item index: " + event.getItemIndex()
				+ ", Column index: " + event.getColumnIndex()
				+ ", Sender index: " + event.getSenderColumnIndex());
		addMessage(message);
	}

	public void handleClose(CloseEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Panel Closed", "Closed panel id:'"
						+ event.getComponent().getId() + "'");
		addMessage(message);
	}

	private void addMessage(FacesMessage message) {
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public DashboardModel getModel() throws SinoPlatformException {
		if (model == null) {
			init();
		}
		return model;
	}

	public List<IViewBean> getAllView() throws SinoPlatformException {
		// TODO
		if (views == null) {
			views = new ArrayList<IViewBean>();

			IViewBean v1 = viewService.createFullViewBean("mainContentHome",
					"mainContentHome-test12", null, "model1");
			IViewBean v2 = viewService.createFullViewBean("mainContentHome",
					"mainContentHome-test10", null, "model2");
			IViewBean v3 = viewService.createFullViewBean("mainContentHome",
					"mainContentHome-test13", null, "model3");
			IViewBean v4 = viewService.createFullViewBean("mainContentHome",
					"mainContentHome-test01", null, "model4");
			IViewBean v5 = viewService.createFullViewBean("mainContentHome",
					"mainContentHome-test14", null, "model5");
			IViewBean v6 = viewService.createFullViewBean("mainContentHome",
					"mainContentHome-test11", null, "model6");
			views.add(v1);
			views.add(v2);
			views.add(v3);
			views.add(v4);
			views.add(v5);
			views.add(v6);
		}
		return views;
	}

	private void init() throws SinoPlatformException {
		model = new DefaultDashboardModel();
		DashboardColumn[] cols = new DashboardColumn[moduleColumn];
		for (int i = 0; i < moduleColumn; i++) {
			cols[i] = new DefaultDashboardColumn();
		}

		getAllView();
		if (views != null)
			for (int i = 0; i < views.size(); i++) {
				cols[i % moduleColumn].addWidget(views.get(i).getId());
			}

		for (int i = 0; i < moduleColumn; i++) {
			model.addColumn(cols[i]);
		}
	}

	public int getModuleColumn() {
		return moduleColumn;
	}

	public void setModuleColumn(int moduleColumn) {
		this.moduleColumn = moduleColumn;
	}

	public PlatformViewService getViewService() {
		return viewService;
	}

	public void setViewService(PlatformViewService viewService) {
		this.viewService = viewService;
	}
	
	

}
